package heap;

import java.util.Comparator;
import java.util.Queue;
import java.util.PriorityQueue;

import java.util.Arrays;

public class PriorityQueueTest {
    public static void main(String[] args) {
        //通过构造方法传入比较器
        //JDK默认最小堆，“值”（比较器compare的返回值）越小，优先级越高
        //传入一个降序的比较器，值(比较器compare的返回值，值越大，返回负数)
        /**
         *  Queue<Student>queue=new PriorityQueue<>(new StudentComDesc())
         * Queue<Student>queue=new PriorityQueue<>(new Comparator<Student>()
         * @Override
         * public int compare(Student o1,Student o2)
         * {
         * return 02.getAge()-01.getAge();
         * }
         */
        Queue<Student> queue = new PriorityQueue<>((o1,o2) -> o2.getAge() - o1.getAge());
        Student stu1 = new Student("铭哥",40);
        Student stu2 = new Student("龙哥",20);
        Student stu3 = new Student("蛋哥",18);
        queue.offer(stu1);
        queue.offer(stu2);
        queue.offer(stu3);
        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
    }
}

class StudentCom implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge() - o2.getAge();
    }
}

class StudentComDesc implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o2.getAge() - o1.getAge();
    }
}

class Student {
    private String name;
    private int age;

    public int getAge() {
        return age;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


